using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace InfoExpress.ESM.WinApp.UserControls
{
  public partial class UcPeriod : UserControl
  {
    #region Members and Properties

    private Dictionary<string, CheckEdit> _checkPeriodMap = new Dictionary<string, CheckEdit>();
    private List<CheckEdit> _checkPeriodList = new List<CheckEdit>();

    private bool _allowGrayed = false;
    private bool _checked = false;
    private CheckState _checkState = CheckState.Unchecked;
    private bool _enabled = true;
    private bool _readOnly = false;
    private DevExpress.Utils.HorzAlignment _glyphAlignment = DevExpress.Utils.HorzAlignment.Center;

    /// <summary>
    /// Periods map to Check boxes.
    /// </summary>
    [Browsable(false)]
    public Dictionary<string, CheckEdit> PeriodMap
    {
      get
      {
        return _checkPeriodMap;
      }
    }

    /// <summary>
    /// Check boxes of Periods list.
    /// </summary>
    [Browsable(false)]
    public List<CheckEdit> PeriodList
    {
      get { return _checkPeriodList; }
    }

    /// <summary>
    /// Gets or sets a value specifying whether a check editor
    /// should support three check states rather than two.
    /// </summary>
    [Browsable(true)]
    [DefaultValue(false)]
    [Description("Sets a value specifying whether a check editor should support three check states rather than two.")]
    public bool AllowGrayed
    {
      get { return _allowGrayed; }
      set 
      { 
        _allowGrayed = value;
        SetThreeState();
      }
    }

    /// <summary>
    /// Gets or sets a value indicating whether a check editor is checked.
    /// </summary>
    [Browsable(true)]
    [DefaultValue(false)]
    [Description("Sets a value indicating whether a check editor is checked.")]
    public bool Checked
    {
      get { return _checked; }
      set
      {
        _checked = value;
        SetChecked();
      }
    }

    /// <summary>
    /// Gets or sets the state of the check editor.
    /// </summary>
    [Browsable(true)]
    [DefaultValue(CheckState.Unchecked)]
    [Description("Sets the state of the check editor.")]
    public CheckState CheckState
    {
      get { return _checkState; }
      set
      {
        _checkState = value;
        if (_checkState == CheckState.Indeterminate)
          AllowGrayed = true;
        else
          AllowGrayed = false;

        SetCheckState();
      }
    }

    /// <summary>
    /// Gets or sets the horizontal alignment of a check box glyph within a check editor.
    /// </summary>
    [Browsable(true)]
    [DefaultValue(DevExpress.Utils.HorzAlignment.Center)]
    [Description("Sets the horizontal alignment of a check box glyph within a check editor.")]
    public DevExpress.Utils.HorzAlignment GlyphAlignment
    {
      get { return _glyphAlignment; }
      set
      {
        _glyphAlignment = value;
        SetHorizontalAlignment();
      }
    }

    /// <summary>
    /// Gets or sets a value indicating whether a check editor is enabled.
    /// </summary>
    [Browsable(true)]
    [DefaultValue(true)]
    [Description("Sets a value indicating whether a check editor is enabled.")]
    public bool CheckEditorEnabled
    {
      get { return _enabled; }
      set
      {
        _enabled = value;
        SetEnabled();
      }
    }

    /// <summary>
    /// Gets or sets a value indicating whether a check editor is read only.
    /// </summary>
    [Browsable(true)]
    [DefaultValue(false)]
    [Description("Sets a value indicating whether a check editor is read only.")]
    public bool ReadOnly
    {
      get { return _readOnly; }
      set
      {
        _readOnly = value;
        SetReadOnly();
      }
    }

    #endregion

    #region Events

    [Browsable(true)]
    public event EventHandler CheckEditCheckChanged;

    #endregion

    public UcPeriod()
    {
      InitializeComponent();
      InitializeDetailComponent();
    }

    #region Helper methods

    private void InitializeDetailComponent()
    {
      int session = 3;
      int period = 6;
      int day = 7;
      int i = 0;
      int j = 0;
      int k = 0;

      //this.tableLayoutPanel1.SuspendLayout();
      this.SuspendLayout();
      int index = 0;
      while (i < day)
      {
        j = 0;
        while (j < session)
        {
          for (k = 0; k < period; k++)
          {
            DevExpress.XtraEditors.PanelControl panelControl;
            DevExpress.XtraEditors.CheckEdit checkEdit;

            panelControl = new DevExpress.XtraEditors.PanelControl();
            checkEdit = new DevExpress.XtraEditors.CheckEdit();


            ((System.ComponentModel.ISupportInitialize)(panelControl)).BeginInit();
            //panelControl.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(checkEdit.Properties)).BeginInit();

            this.tableLayoutPanel1.Controls.Add(panelControl, i + 1, k + j * period + 1 + j * 1);
            // 
            // panelControl
            // 
            panelControl.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
            panelControl.Controls.Add(checkEdit);
            panelControl.Dock = System.Windows.Forms.DockStyle.Fill;
            panelControl.Location = new System.Drawing.Point(94, 26);
            panelControl.Margin = new System.Windows.Forms.Padding(1);
            panelControl.Name = "panelControl_" + i + "_" + j + "_" + k;
            panelControl.Size = new System.Drawing.Size(91, 23);
            panelControl.TabIndex = 25;
            // 
            // checkEdit
            // 
            checkEdit.Dock = System.Windows.Forms.DockStyle.Fill;
            checkEdit.Location = new System.Drawing.Point(2, 2);
            checkEdit.Name = i + "_" + j + "_" + k;
            checkEdit.Properties.Caption = "";
            checkEdit.Properties.GlyphAlignment = DevExpress.Utils.HorzAlignment.Center;
            checkEdit.Size = new System.Drawing.Size(87, 19);
            checkEdit.TabIndex = index;
            checkEdit.CheckStateChanged += new EventHandler(checkEdit_CheckStateChanged);
            _checkPeriodMap.Add(checkEdit.Name, checkEdit);
            _checkPeriodList.Add(checkEdit);

            ((System.ComponentModel.ISupportInitialize)(panelControl)).EndInit();
            //panelControl.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(checkEdit.Properties)).EndInit();

            index++;
          }
          j++;
        }
        i++;
      }
      this.ResumeLayout(false);
      //this.tableLayoutPanel1.ResumeLayout(false);
    }

    

    private void SetThreeState()
    {
      foreach (CheckEdit checkEdit in _checkPeriodList)
      {
        checkEdit.Properties.AllowGrayed = _allowGrayed;
      }
    }

    private void SetChecked()
    {
      foreach (CheckEdit checkEdit in _checkPeriodList)
      {
        checkEdit.Checked = _checked;
      }
    }

    private void SetCheckState()
    {
      foreach (CheckEdit checkEdit in _checkPeriodList)
      {
        checkEdit.CheckState = _checkState;
      }
    }

    private void SetHorizontalAlignment()
    {
      foreach (CheckEdit checkEdit in _checkPeriodList)
      {
        checkEdit.Properties.GlyphAlignment = _glyphAlignment;
      }
    }

    private void SetEnabled()
    {
      foreach (CheckEdit checkEdit in _checkPeriodList)
      {
        checkEdit.Enabled = _enabled;
      }
    }

    private void SetReadOnly()
    {
      foreach (CheckEdit checkEdit in _checkPeriodList)
      {
        checkEdit.Properties.ReadOnly = _readOnly;
      }
    }

    #endregion

    #region Public methods

    /// <summary>
    /// Clear check editors.
    /// </summary>
    public void ClearPeriodCheck()
    {
      foreach (CheckEdit checkEdit in _checkPeriodList)
      {
        checkEdit.Checked = false;
      }
    }

    /// <summary>
    /// Clear all text in check editors.
    /// </summary>
    public void ClearCheckEditText()
    {
      foreach (CheckEdit checkEdit in _checkPeriodList)
      {
        checkEdit.Text = string.Empty;
      }
    }

    /// <summary>
    /// Get checked box name list.
    /// </summary>
    /// <returns></returns>
    public List<string> GetCheckedNameList()
    {
      List<string> checkedNameList = new List<string>();
      foreach (KeyValuePair<string, CheckEdit> checkEdit in _checkPeriodMap)
      {
        if(checkEdit.Value.Checked)
          checkedNameList.Add(checkEdit.Key);
      }

      return checkedNameList;
    }

    #endregion

    #region Event handlers

    void checkEdit_CheckStateChanged(object sender, EventArgs e)
    {
      if (CheckEditCheckChanged != null)
      {
        CheckEditCheckChanged(sender, e);
      }
    }

    #endregion
  }
}
